package com.sgb.kjwl.view.ui.activity.self.publish;

import android.content.Context;
import android.content.Intent;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityMachineListBinding;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.self.MachinePublishEvent;
import com.sgb.kjwl.view.ui.fragment.self.MachineStateFragment;
import com.swgk.core.recyclerview.adapter.SlidingFragmentPagerAdapter;
import com.swgk.core.util.SharedPreferenceUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

/**
 * 我的-租赁经营-机械发布
 */
public class MachineListActivity extends AppActivity {

    private ActivityMachineListBinding binding;
    private String[] titles = {"已上架", "已下架", "草稿"};
    private ArrayList<Fragment> mFragments;

    public static void start(Context context) {
        start(context, null);
    }

    public static void start(Context context, Intent extras) {
        Intent intent = new Intent();
        intent.setClass(context, MachineListActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        if (extras != null) {
            intent.putExtras(extras);
        }
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(MachineListActivity.this, R.layout.activity_machine_list);
        binding.topLayout.ivBack.setOnClickListener(v -> finish());
        binding.topLayout.tvRight.setOnClickListener(v -> LeaseMachineActivity.start(MachineListActivity.this));
        binding.topLayout.editSearch.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        binding.topLayout.editSearch.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                    closeKeybord();
                    EventBus.getDefault().post(MachinePublishEvent.make().setMessageTag(EventConstant.EVENT_PUBLISH_MACHINE_REFUSH));
                }
                return false;
            }
        });
    }

    public String getKeyWord() {
        return binding.topLayout.editSearch.getText().toString();
    }

    @Override
    protected void init() {
        //0草稿，1 上架 2 下架 3 审核不过，4 审核中
        mFragments = new ArrayList<Fragment>();
        mFragments.add(MachineStateFragment.newInstance("1"));
        mFragments.add(MachineStateFragment.newInstance("2"));
        //如果是企业用户 则 增加3 审核不过，4 审核中
        String myCompanyNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, "");
        if (!TextUtils.isEmpty(myCompanyNo) && !"0".equals(myCompanyNo)) {
            mFragments.add(MachineStateFragment.newInstance("4"));
            mFragments.add(MachineStateFragment.newInstance("3"));
            titles = new String[]{"已上架", "已下架", "审批中", "已拒绝", "草稿"};
        }
        mFragments.add(MachineStateFragment.newInstance("0"));
        SlidingFragmentPagerAdapter mAdapetr = new SlidingFragmentPagerAdapter(getSupportFragmentManager(), mFragments, titles);
        binding.viewpager.setAdapter(mAdapetr);
        binding.viewpager.setOffscreenPageLimit(2);
        binding.tablayout.setupWithViewPager(binding.viewpager);
    }

}

